package com.front.admin;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@Slf4j
public class LoginController {

    private RestTemplate restTemplate = new RestTemplate();
    @ResponseBody
    @RequestMapping("/login")
    public String login(@RequestBody Credentials credentials, HttpServletRequest request) {

        System.out.println(credentials.getUsername() + ":" + credentials.getPassword());
        String oauthServiceURL = "http://gateway.bingo.com:80/token/oauth/token";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.setBasicAuth("admin", "123456");
        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
        params.add("username", credentials.getUsername());
        params.add("password", credentials.getPassword());
        params.add("grant_type", "password");
        params.add("scope", "read write");
        HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity(params, headers);
        ResponseEntity<TokenInfo> response = restTemplate.exchange(oauthServiceURL, HttpMethod.POST, entity,
                TokenInfo.class);
        request.getSession().setAttribute("token", response.getBody());
        return "success";
    }

    @GetMapping("/me")
    @ResponseBody
    public TokenInfo me(HttpServletRequest request) {
        TokenInfo info = (TokenInfo)request.getSession().getAttribute("token");
        return info;
    }


    @GetMapping("/oauth/callback")
    public void callback (@RequestParam String code, String state, HttpServletRequest request, HttpServletResponse response) throws IOException {

        log.info("state is "+state);
        String oauthServiceUrl = "http://gateway.bingo.com:80/token/oauth/token";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.setBasicAuth("admin", "123456");

        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("code", code);
        params.add("grant_type", "authorization_code");
        params.add("redirect_uri", "http://admin.bingo.com:8083/oauth/callback");
        HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(params, headers);
        ResponseEntity<TokenInfo> token = restTemplate.exchange(oauthServiceUrl, HttpMethod.POST, entity, TokenInfo.class);
		request.getSession().setAttribute("token", token.getBody().init());
        response.sendRedirect("/");
    }

    @RequestMapping("logout")
    public void logout(HttpServletRequest request, HttpServletResponse response){
        log.info("log out.....");
        request.getSession().invalidate();
    }
}
